package co.yixiang.yshop.module.order.service.storeordercartinfo;

import cn.hutool.core.util.IdUtil;
import co.yixiang.yshop.framework.common.enums.OrderInfoEnum;
import co.yixiang.yshop.module.order.dal.dataobject.storeordercartinfo.StoreOrderCartInfoDO;
import co.yixiang.yshop.module.order.dal.mysql.storeordercartinfo.StoreOrderCartInfoMapper;
import co.yixiang.yshop.module.product.controller.app.cart.vo.AppStoreCartQueryVo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单购物详情 Service 实现类
 *
 * @author yshop
 */
@Slf4j
@Service
@Validated
public class StoreOrderCartInfoServiceImpl extends ServiceImpl<StoreOrderCartInfoMapper, StoreOrderCartInfoDO> implements StoreOrderCartInfoService {

    /**
     * 添加购物车商品信息
     *
     * @param oid      订单id
     * @param orderId  订单号
     * @param cartInfo 购物车信息
     */
    @Async
    @Override
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCartInfo(Long oid, String orderId, List<AppStoreCartQueryVo> cartInfo) {
        log.info("==========添加购物车商品信息start===========");
        List<StoreOrderCartInfoDO> list = new ArrayList<>();
        for (AppStoreCartQueryVo cart : cartInfo) {
            StoreOrderCartInfoDO info = new StoreOrderCartInfoDO();
            info.setOid(oid);
            info.setOrderId(orderId);
            info.setCartId(cart.getId());
            info.setProductId(cart.getProductId());
            info.setUnique(IdUtil.simpleUUID());
            info.setIsAfterSales(OrderInfoEnum.YES.getValue());
            info.setCouponPrice(cart.getCouponPrice());
            info.setPostagePrice(cart.getPostagePrice());
            info.setIntegralDeductionAmount(cart.getIntegralDeductionAmount());
            info.setUseIntegral(cart.getUseIntegral());
            info.setPayPrice(cart.getPayPrice());

            info.setCartInfo(JSONObject.toJSON(cart).toString());
            list.add(info);
        }
        saveBatch(list);
    }

}
